<?php
/*

___Canvas Export Functions______________________________________

Outputs your current layout to an XML file for sharing.

________________________________________________________________

*/

function canvas_export($author='', $title='', $uri='', $output = 'link', $incl_vars = true, $delete=false) {
	global $wpdb, $canvas;
	
	if($title == '') $title = 'Canvas Layout';
	if($author == '') $author = get_bloginfo('');
	if($uri == '') $uri = get_bloginfo('siteurl');
	$layout_info = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE zone != 'shelf' AND theme = '".get_option('template')."' ORDER BY block_id");
	$var_info = $wpdb->get_results("SELECT variable_name, value, parent FROM ".$canvas->variables." LEFT OUTER JOIN ".$canvas->main." ON parent = block_id WHERE zone != 'shelf' AND theme = '".get_option('template')."' ORDER BY parent");

	$filename = 'Canvas_'.date('d.M.Y').'.xml';
	$fp = ABSPATH.$filename;
	$handle = fopen($fp, 'w+') or die("ERROR: Can't create XML file: ".$fp.". No write access to the server.");

	$content = '<?xml version="1.0"?>'."\n";
	$content .= '<canvas>'."\n";
	$content .= "\t".'<export>'."\n";
	$content .= "\t\t".'<meta>'."\n";
	$content .= "\t\t\t".'<title>'."\n";
	$content .= "\t\t\t".$title."\n";
	$content .= "\t\t\t".'</title>'."\n";
	$content .= "\t\t\t".'<author>'."\n";
	$content .= "\t\t\t".$author."\n";
	$content .= "\t\t\t".'</author>'."\n";
	$content .= "\t\t\t".'<theme>'."\n";
	$content .= "\t\t\t".get_option('template')."\n";
	$content .= "\t\t\t".'</theme>'."\n";
	$content .= "\t\t\t".'<uri>'."\n";
	$content .= "\t\t\t".$uri."\n";
	$content .= "\t\t\t".'</uri>'."\n";
	$content .= "\t\t".'</meta>'."\n";
	$content .= "\t\t".'<layout>'."\n";

	foreach($layout_info as $block) {
		if(strlen($block->ubi) != 32) {
			echo 'One or more of your blocks has an invalid UBI and cannot be exported. Export failed.';
			exit;
		}
		$content .= "\t\t\t".'<block>'."\n";
		$content .= "\t\t\t\t".'<id>'."\n";
		$content .= "\t\t\t\t".$block->block_id."\n";
		$content .= "\t\t\t\t".'</id>'."\n";
		$content .= "\t\t\t\t".'<type>'."\n";
		$content .= "\t\t\t\t".$block->type."\n";
		$content .= "\t\t\t\t".'</type>'."\n";
		$content .= "\t\t\t\t".'<zone>'."\n";
		$content .= "\t\t\t\t".$block->zone."\n";
		$content .= "\t\t\t\t".'</zone>'."\n";
		$content .= "\t\t\t\t".'<position>'."\n";
		$content .= "\t\t\t\t".$block->position."\n";
		$content .= "\t\t\t\t".'</position>'."\n";
		$content .= "\t\t\t\t".'<ubi>'."\n";
		$content .= "\t\t\t\t".$block->ubi."\n";
		$content .= "\t\t\t\t".'</ubi>'."\n";

		if($incl_vars) {
			$content .= "\t\t\t\t".'<variables>'."\n";
			foreach($var_info as $variable) {
				if($block->block_id == $variable->parent) {
					$content .= "\t\t\t\t\t".'<variable>'."\n";
					$content .= "\t\t\t\t\t\t".'<name>'."\n";
					$content .= "\t\t\t\t\t\t".$variable->variable_name."\n";
					$content .= "\t\t\t\t\t\t".'</name>'."\n";
					$content .= "\t\t\t\t\t\t".'<value>'."\n";
					if($variable->value) $content .= "\t\t\t\t\t\t".$variable->value."\n";
					$content .= "\t\t\t\t\t\t".'</value>'."\n";
					$content .= "\t\t\t\t\t".'</variable>'."\n";
				}
			}
			$content .= "\t\t\t\t".'</variables>'."\n";
		}

		$content .= "\t\t\t".'</block>'."\n";
	}

	$content .= "\t\t".'</layout>'."\n";
	$content .= "\t".'</export>'."\n";
	$content .= '</canvas>'."\n";

	if(!fwrite($handle, $content)) {
		echo "ERROR: Cannot write to XML file ($fp).";
		exit;
	}

	fclose($handle);

	if($output == 'link') {
		echo 'Processing complete.<br/><a href="'.get_bloginfo('wpurl').'/'.$filename.'">Download</a> your layout file.';
	} elseif($output == 'file') {
		header("Pragma: public"); // required
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false); // required for certain browsers 
		header("Content-Type: application/xml");
		header("Content-Disposition: attachment; filename=\"".basename($fp)."\";" );
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".filesize($fp));
		readfile("$fp");
	}

	if($delete) unlink($fp);
	exit();
}

function canvas_export_form() {
?>
<div class="lbContent">
<h3>Export to XML
<p>You can export your Canvas layout and share it with other Canvas users.</p>
<a id="cancel" href="javascript:void(0)" class="lbAction" rel="cancel">Cancel</a>
</h3>
<div>
	<form id="canvas_export">
		<label class="textbox">Layout Title: <input type="text" class="text" name="layout_title" id="layout_title" /></label>
		<label class="textbox">Author: <input type="text" class="text" name="layout_author" id="layout_author" /></label>
		<label class="textbox">URI: <input type="text" class="text" name="layout_uri" id="layout_uri" /></label>
		<input type="button" id="export_canvas" name="export_canvas" value="Export" onclick="Canvas.exportXml(Form.serialize('canvas_export'))"/>
	</form>
	<p id="export_message"></p>
</div>
</div>

<?php
}

if($_GET["canvas_export_form"] == 'true') canvas_export_form();
?>